package de.tabuma.formcreator.javascript.logik;

import de.tabuma.formcreator.*;
import de.tabuma.formcreator.entity.*;
import de.tabuma.formcreator.logik.*;

import java.util.logging.*;
import java.util.*;

/**
 *
 * @author $Author: $
 * @version $Revision: $
 * @since $Date: $
 */
public class ViewFacade {
    
    private static final Logger log = Logger.getLogger(ViewFacade.class.getName());

    public static String getAllView(FormStructur formStructur, boolean editable) {
        String str = "";

        str += "\t<!-- Start of all view methods for object"+formStructur.getId()+"("+formStructur.getName()+") -->\n";

        str += "\n";

        str += "\tvar "+JSLib.getDescriptionVar(formStructur)+" = '';\n";

        str += printView(formStructur, editable);
        str += "\n";

        str += getRemoveOkView(formStructur);
        str += "\n";

        str += getRemoveView(formStructur);
        str += "\n";

        str += getListContent(formStructur);
        str += "\n";

        str += "\t<!-- End of all view methods for object"+formStructur.getId()+"("+formStructur.getName()+") -->\n";
        return str;
    }

    /**
     * Creates a JS function that prints layout elements for displaying a formStructur on Screen / in Browser.<br/><br/>
     * <pre>
     *  <code>
     *      ---fieldset.fieldset---------------------------------
     *      |                                                   |
     *      |           -----------------                       |
     *      | Field1    |               |                       |
     *      |           -----------------                       |
     *      |           -----------------                       |
     *      | Field2    |               |                       |
     *      |           -----------------                       |
     *      |                                                   |
     *      | $ListElements                                     |
     *      |                                                   |
     *      --div.formularbottom---------------------------------
     *      | -input.buttonNextClass- -input.buttonCancelClass- |
     *      | | Save Button         | | Cancel Button         | |
     *      | ----------------------- ------------------------- |
     *      -----------------------------------------------------
     *  </code>
     * </pre>
     * By clicking the buttons (Save Button, Cancel Button) same controllers were running (SaveController and CancelController).
     * @param formStructur FormStructur that should be displayed
     * @return String in JS Format
     */
    public static String printView(FormStructur formStructur, boolean editable) {
        int j=-1;
        for(int i=0;i<formStructur.getHeadStructur().getFormStructur().size();i++) {
            if(formStructur.getHeadStructur().getFormStructur().get(i).getId()==formStructur.getId()) {
                j = i;
            }
        }

        String str = "\tfunction "+JSLib.getPrintView(formStructur)+"() {\n";
        str += "\t\tform = '"+BreadcrumpsFacade.printBreadcrumps(formStructur)+"';\n";
        str += "\t\tform += '<fieldset class=\"fieldset\">';\n";
        str += "\t\tform += '<legend>"+formStructur.getName()+"</legend>';\n";
        str += "\t\tif("+JSLib.getDescriptionVar(formStructur)+"!='') {\n";
        str += "\t\t\tform += '<div class=\"descriptionClass\">';\n";
        str += "\t\t\tform += '<div class=\"descriptionClass1\">';\n";
        str += "\t\t\tform += '<div class=\"descriptionContentClass\">'+"+JSLib.getDescriptionVar(formStructur)+"+'</div>';\n";
        str += "\t\t\tform += '<div class=\"descriptionCloseImageClass\"><a href=\"javascript:parent."+JSLib.getUnshowDescriptionControllerName(formStructur)+"()\"><img name=\"infoCloseIcon - "+formStructur.getName()+"\" class=\"infoCloseIcon\"/></a></div>';\n";
        str += "\t\t\tform += '</div>';\n";
        str += "\t\t\tform += '</div>';\n";
        str += "\t\t}\n";
        str += "\t\tform += '<div class=\"fieldset_"+formStructur.getXMLName()+"\">';\n";
        //Going through structur and getting all simple types
        for(int i=0;i<formStructur.getFormStructur().size();i++) {
            FormStructur fs = formStructur.getFormStructur().get(i);
            if(fs.isSimpleType()) {
                str += "\t\tform += '"+getElementView(fs, editable)+"';\n";
            }
            if(fs.hasMinAndMaxOccurs1()) {
                for(int l=0;l<fs.getFormStructur().size();l++) {
                    FormStructur fs1 = fs.getFormStructur().get(l);
                    if(fs1.isSimpleType()) {
                        str += "\t\tform += '"+getElementView(fs1, editable)+"';\n";
                    }
                }
            }
        }
        str += "\t\tform += '</div>';\n";

        if(formStructur.getFormStructur().size()>0) {
            //Going through structur and getting all complex types
            for(int i=0;i<formStructur.getFormStructur().size();i++) {
                FormStructur fs = formStructur.getFormStructur().get(i);
                if(!fs.isSimpleType()) {
                    if(!fs.hasMinAndMaxOccurs1()) {
                        str += getListView(fs, editable);
                    }
                    else {
                        for(int i1=0;i1<fs.getFormStructur().size();i1++) {
                            FormStructur fs1 = fs.getFormStructur().get(i1);
                            if(!fs1.isSimpleType()) {
                                str += getListView(fs1, editable);
                            }
                        }
                    }
                }
            }
        }
        str += "\t\tform += '</fieldset>';\n";
        str += "\t\tform += '<div class=\"formularbottom\">';\n";

        String buttonSend = constants.getValueFromProperties("de.tabuma.formcreator.javascript.view.button.send");
        String buttonNext = constants.getValueFromProperties("de.tabuma.formcreator.javascript.view.button.next");
        String buttonCancel = constants.getValueFromProperties("de.tabuma.formcreator.javascript.view.button.cancel");

        if(editable) {
            if(formStructur.getHeadStructur().getFormStructur().size()==j+1) {
                str += "\t\tform += '<input type=\"button\" name=\"Send\" value=\""+buttonSend+"\" class=\"buttonNextClass\" onClick=\"javascript:parent."+JSLib.getSendDataControllerName()+"()\"/>';\n";
            }
            else {
                str += "\t\tform += '<input type=\"button\" name=\"Next\" value=\""+buttonNext+"\" class=\"buttonNextClass\" onClick=\"javascript:parent."+JSLib.getSaveControllerName(formStructur)+"()\"/>';\n";
            }
        }
        if(formStructur.getHeadStructur().getHeadStructur()!=null) {
            str += "\t\tform += '<input type=\"button\" name=\"Cancel\" value=\""+buttonCancel+"\" class=\"buttonCancelClass\" onClick=\"javascript:parent."+JSLib.getSaveCancelControllerName(formStructur)+"()\"/>';\n";
        }
        str += "\t\tform += '</div>';\n";

        str += "\t\t"+JSLib.getPrintIframe()+"(form);\n";
        str += "\t}\n";
        return str;
    }

    /**
     * This method print if formStructur is simpleType an input field which means element view.
     * Code looks like:
     * <pre>
     *  <code>
     *      -----div.lineClass---------------------------
     *      |  --div.labelClass--   --div.inputClass--  |
     *      |  | Labeltext (o)  |   | input...       |  |
     *      |  ------------------   ------------------  |
     *      ---------------------------------------------
     *  </code>
     * </pre>
     *
     * (o) meens that there is an documentation code
     *
     * @param formStructur Structur that will be created as element view
     * @return string with element view
     */
    public static String getElementView(FormStructur formStructur, boolean editable) {
        String xForm = "";
        if(formStructur.isInput()) {

            xForm += "<div class=\"lineClass line_"+formStructur.getHeadStructur().getXMLName()+"_"+formStructur.getXMLName()+"_Class\">";

            //TODO adding handling for description

            
            if(formStructur.getDescription()!=null && !formStructur.getDescription().equals("")) {
                xForm += "<div class=\"labelClass label"+formStructur.getHeadStructur().getXMLName()+"_"+formStructur.getXMLName()+"Class\">";
                xForm += "<div class=\"labelClassContent\">"+formStructur.getName()+"</div>";
                xForm += "<div class=\"labelClassIcon\"><a href=\"javascript:parent."+JSLib.getShowDescriptionControllerName(formStructur)+"()\" class=\"infoIcon\"><img name=\"infoIcon - "+formStructur.getName()+"\" class=\"infoIcon\"/></a></div></div>";
            }
            else {
                xForm += "<div class=\"labelClass label"+formStructur.getHeadStructur().getXMLName()+"_"+formStructur.getXMLName()+"Class\">";
                xForm += formStructur.getName();
                xForm += "</div>";
            }
            
            if(editable) {
                xForm += "<div class=\"inputClass input"+formStructur.getId()+"Class\">";
                xForm += "<input type=\"text\" name=\""+JSLib.getElementName(formStructur)+"\" class=\"inputClass input"+formStructur.getHeadStructur().getXMLName()+"_"+formStructur.getXMLName()+"Class\" value=\"'+"+JSLib.getObjectName(formStructur.getHeadStructur())+"."+JSLib.getObjectGetterName(formStructur)+"()"+"+'\"/>";
                xForm += "</div>";
            }
            else {
                xForm += "<div class=\"inputClass input"+formStructur.getId()+"Class\">";
                xForm += "<div class=\"showElementClass showElement"+formStructur.getHeadStructur().getXMLName()+"_"+formStructur.getXMLName()+"Class\">'+"+JSLib.getObjectName(formStructur.getHeadStructur())+"."+JSLib.getObjectGetterName(formStructur)+"()"+"+'</div>";
                xForm += "</div>";
            }
            xForm += "</div>";
            return xForm;
        }
        return xForm;
    }

    /**
     * This method creates a javascript function that print in iframe an &quot;Do you really want to remove ...&quot;
     * <pre>
     *  <code>
     *      --div.classRemoveAll------------------------------------------------
     *      | --div.classRemove-------------------------------------------     |
     *      | | Do you really ...                                        |     |
     *      | ------------------------------------------------------------     |
     *      | -input.buttonRemoveClass-  -input.buttonRemoveCancelClass-       |
     *      | | Remove it             |  | Cancel                      |       |
     *      | -------------------------  -------------------------------       |
     *      --------------------------------------------------------------------
     *  </code>
     * </pre>
     * @param formStructur Structur for that the message will be displayed
     * @return String with the javascript function
     */
    public static String getRemoveView(FormStructur formStructur) {
        String statement = constants.getValueFromProperties("de.tabuma.formcreator.javascript.remove.statement");
        String button = constants.getValueFromProperties("de.tabuma.formcreator.javascript.remove.button");
        String cancel = constants.getValueFromProperties("de.tabuma.formcreator.javascript.remove.cancel");
        String str = "\tfunction "+JSLib.getRemoveView(formStructur)+"() {\n";
        str += "\t\tform = '';\n";
        str += "\t\tform += '<div class=\"classRemoveAll\">';\n";
        str += "\t\tform += '<div class=\"classRemove\">"+statement+"</div><br>';\n";
        str += "\t\tform += '<input type=\"button\" class=\"buttonRemoveClass\" onClick=\"javascript:parent."+JSLib.getRemoveControllerName(formStructur)+"\" value=\""+button+"\"/>';\n";
        str += "\t\tform += '<input type=\"button\" class=\"buttonRemoveCancelClass\" onClick=\"javascript:parent."+JSLib.getRemoveCancelControllerName(formStructur)+"\" value=\""+cancel+"\"/>';\n";
        str += "\t\tform += '</div>';\n";
        str += "\t\t"+JSLib.getPrintIframe()+"(form);\n";
        str += "\t}\n";
        return str;
    }

    /**
     * This method creates a javascript function that shows that removing element was succesfuss.
     * <pre>
     *  <code>
     *      --div.classRemove1All-------------------------
     *      | --div.classRemove1--------------------     |
     *      | | remove text                        |     |
     *      | --------------------------------------     |
     *      | --input.buttonRemove--                     |
     *      | | next               |                     |
     *      | ----------------------                     |
     *      ----------------------------------------------
     *  </code>
     * </pre>
     * @param formStructur Element that will be removed.
     * @return String with javascript function
     */
    public static String getRemoveOkView(FormStructur formStructur) {
        String message = constants.getValueFromProperties("de.tabuma.formcreator.javascript.remove.message");
        String further = constants.getValueFromProperties("de.tabuma.formcreator.javascript.remove.further");
        String str = "<div class=\"classRemove1All\"><div class=\"classRemove1\">"+message+"</div>";
        str += "<input type=\"button\" class=\"buttonRemove\" value=\""+further+" onClick=\"javascript:parent."+JSLib.getRemoveController1Name(formStructur)+"\"/>";
        str += "</div></div>";

        String function = "\tfunction printRemoveOk"+formStructur.getId()+"() {\n";
        function += "\t\tvalue = '" + str+"';\n";
        function += "\t\t" + JSLib.getPrintIframe()+"(value);\n";
        function += "\t}\n";

        return function;
    }

    /**
     * This method prints a javascript function, that creates list content.
     * <pre>
     *  <code>
     *      --div.listViewClass-----------------------------------
     *      |  --fieldset.fieldset1----------------------------  |
     *      |  |  --table.tableClass------------------------  |  |
     *      |  |  |  -tr---------------------------------  |  |  |
     *      |  |  |  |  -td.tableHeadClass------------  |  |  |  |
     *      |  |  |  |  | field1, field2, ....       |  |  |  |  |
     *      |  |  |  |  ------------------------------  |  |  |  |
     *      |  |  |  |  -td.tableHeadRemoveClass------  |  |  |  |
     *      |  |  |  |  | remove text                |  |  |  |  |
     *      |  |  |  |  ------------------------------  |  |  |  |
     *      |  |  |  -----------------------------------|  |  |  |
     *      |  |  |  -tr---------------------------------  |  |  |
     *      |  |  |  |  -td.tableContentClass---------  |  |  |  |
     *      |  |  |  |  | field1, field2, ....       |  |  |  |  |
     *      |  |  |  |  ------------------------------  |  |  |  |
     *      |  |  |  |  -td.tableContentRemoveClass---  |  |  |  |
     *      |  |  |  |  | remove link text (linkClass)  |  |  |  |
     *      |  |  |  |  ------------------------------  |  |  |  |
     *      |  |  |  ------------------------------------  |  |  |
     *      |  |  |  ...                                   |  |  |
     *      |  |  ------------------------------------------  |  |
     *      |  |  --input.buttonAddListElement--              |  |
     *      |  |  |  add text                  |              |  |
     *      |  |  ------------------------------              |  |
     *      |  ------------------------------------------------  |
     *      ------------------------------------------------------
     *  </code>
     * </pre>
     *
     *
     * @param formStructur Structur that will be displayed as list
     * @return String with javascript function
     */
    public static String getListView(FormStructur formStructur, boolean editable) {

        String remove = constants.getValueFromProperties("de.tabuma.formcreator.javascript.view.list.remove.label");
        String removeElement = constants.getValueFromProperties("de.tabuma.formcreator.javascript.view.list.remove.element");
        String addButton = constants.getValueFromProperties("de.tabuma.formcreator.javascript.view.list.add.button");

        String tableHead = "<table class=\"tableClass\"><tr><td class=\"tableHeadClass\">";
        boolean first = true;
        for(int i=0;i<formStructur.getFormStructur().size();i++) {
            FormStructur fs = formStructur.getFormStructur().get(i);
            if(fs.isSimpleType()) {
                if(!first) {
                    tableHead += ", ";
                }
                else {
                    first = false;
                }
                tableHead += fs.getName();
            }
        }
        if(editable) {
            tableHead += "</td><td class=\"tableHeadRemovedClass\">"+remove+"</td></tr>";
        }

        String noentry = constants.getValueFromProperties("de.tabuma.formcreator.javascript.view.list.noentry");

        String table = "";
        table += "\t\tform += '<div class=\"listViewClass\">'\n";
        table += "\t\tform += '<fieldset class=\"fieldset1\">';\n";
        table += "\t\tform += '<legend>"+formStructur.getName()+"</legend>';\n";
        table += "\t\tform += '"+tableHead+"';\n";
        table += "\t\tarrayList = " + JSLib.getObjectName(formStructur.getHeadStructur())+"."+JSLib.getObjectGetterName(formStructur)+"();\n";
        table += "\t\tfor(i=0;i<arrayList.length;i++) {\n";
        table += "\t\t\tmod = i % 2 + 1;\n";
        table += "\t\t\telement = arrayList[i];\n";
        table += "\t\t\tform += '<tr><td class=\"tableContent'+mod+'Class\">';\n";
        table += "\t\t\tform += '<a href=\"javascript:parent."+JSLib.getEditElementControllerName(formStructur)+"('+i+')\" class=\"linkClass\">'+"+JSLib.getListContentName(formStructur)+"(element) +'</a>';\n";
        table += "\t\t\tform += '</td>';\n";
        if(editable) {
            table += "\t\t\tform += '<td class=\"tableContentRemove'+mod+'Class\"><a href=\"javascript:"+JSLib.getStartRemoveControllerName(formStructur)+"('+ i + ')\" class=\"linkClass\">"+removeElement+"</a></td>';\n";
        }
        table += "\t\t\tform += '</tr>';\n";
        table += "\t\t}\n";
        table += "\t\tif(arrayList.length==0) {\n";
        table += "\t\t\tform += '<tr><td class=\"tableContent1Class\">"+noentry+"</td>';\n";
        if(editable) {
            table += "\t\t\tform += '<td class=\"tableContentRemove1Class\"></td>';\n";
        }
        table += "\t\t\tform += '</tr>';\n";
        table += "\t\t}\n";
        table += "\t\tform += '</table>';\n";
        if(editable) {
            table += "\t\tform += '<br/><input type=\"button\" class=\"buttonAddListElement\" value=\""+addButton+"\" onClick=\"javascript:parent."+JSLib.getAddElementControllerName(formStructur)+"()\"/>';\n";
        }
        table += "\t\tform += '</fieldset>';\n";
        table += "\t\tform += '</div>';\n";
        return table;
    }

    /**
     * Prints javascript function for creating a comma seperated list (value1, value2, ...) of a object
     * @param formStructur Object that will be seperated in a list
     * @return String with javascript Function
     */
    public static String getListContent(FormStructur formStructur) {
        String str = "\tfunction "+JSLib.getListContentName(formStructur)+"(a) {\n";
        str += "\t\tform = '';\n";
        boolean hasSimpleElements = false;
        for(int i=0;i<formStructur.getFormStructur().size();i++) {
            FormStructur fs = formStructur.getFormStructur().get(i);
            if(fs.isSimpleType()) {
                if(hasSimpleElements) {
                    str += "\t\tform += ', ';\n";
                }
                else {
                    hasSimpleElements = true;
                }
                str += "\t\tform += a."+JSLib.getObjectGetterName(fs)+"();\n";
            }
        }
        str += "\t\treturn form;\n";
        str += "\t}\n";
        return str;
    }

    
}
